﻿layui.use(['form', 'layedit', 'laydate',  'layer'], function () {

    var form = layui.form, layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery, player = parent.layer || layui.layer,
        laydate = layui.laydate;

    var projectInfoModifyLogic = {
        //初始化
        init: function () {

            self = this;

            this.Parameters = {
                ac: new AsyncCaller(),    //异步方法管理
                projectTypeSelectUrl: "/Project/Info/LoadProjectTypeSelect",
                contractSelectUrl: "/Project/Info/LoadContractSelect",
                saveUrl: "/Project/Info/ModifyInfo",
            };

            this.initPage();

            this.bindEvent();

        },
        //初始化列表
        initPage: function () {
            //日期
            var startTime = laydate.render({
                elem: '#StartDate',
                done: function (value, date, endDate) {
                    endLayDate.config.min = {
                        year: date.year,
                        month: date.month - 1,
                        date: date.date
                        /* hours: date.hours,
                           minutes: date.minutes,
                           seconds: date.seconds +1*/
                    };
                }
            });

            var endLayDate = laydate.render({
                elem: '#EndDate',
                done: function (value, date, endDate) {
                    startTime.config.max = {
                        year: date.year != undefined ? date.year : 2099,
                        month: (date.month != undefined) ? (date.month - 1) : 12,
                        date: (date.date != undefined) ? date.date : 25
                        /* hours: date.hours,
                           minutes: date.minutes,
                           seconds: date.seconds -1*/
                    };
                }
            });

            this.Parameters.ac
                .pushQueue(function () {
                    self.logicFunc.loadProjectTypeSelect(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    });
                })
                .pushQueue(function () {
                    self.logicFunc.loadContractSelect(function () {
                        self.Parameters.ac.notifyQueueAsyncFuncComplate();
                    });
                })
                .pushQueue(function () {
                    //获取信息
                    var data = GrdAssist.queryString("data");
                    self.logicFunc.loadInfo(data);

                    form.render();
                })
                .exec();
        },
        //绑定事件
        bindEvent: function () {

            //监听立即修改提交
            form.on('submit(modifyButton)', self.logicFunc.saveInfo);

            //关闭
            $("#btnClose").on("click", self.logicFunc.close);
        },
        //逻辑方法
        logicFunc: {

            //绑定工程类型
            loadProjectTypeSelect: function (func) {

                GrdLayUI.ajaxPost({
                    url: self.Parameters.projectTypeSelectUrl,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            var data = result.Data;
                            $("#ProjectTypeID").empty();
                            for (var i = 0; i < data.length; i++) {
                                $("#ProjectTypeID").append("<option value=" + data[i].Value + ">" + data[i].Text + "</option>");
                            }
                            if (func) {
                                func();
                            }
                        }
                    }
                });
            },

            //承包性质
            loadContractSelect: function (func) {

                GrdLayUI.ajaxPost({
                    url: self.Parameters.contractSelectUrl,
                    data: "",
                    success: function (result) {
                        if (result.Code == 1000) {
                            var data = result.Data;
                            $("#Contract").empty();
                            for (var i = 0; i < data.length; i++) {
                                $("#Contract").append("<option value=" + data[i].Value + ">" + data[i].Text + "</option>");
                            }
                            if (func) {
                                func();
                            }
                        }
                    }
                });
            },

            //获取修改的信息
            loadInfo: function (data) {

                GrdLayUI.assignmentData("#modify-form", data);
            },

            ///保存数据
            saveInfo: function () {
                var json = {
                    parameters: GrdLayUI.initParamsData($("#modify-form"))
                };

                GrdLayUI.ajaxPost({
                    url: self.Parameters.saveUrl,
                    data: json,
                    success: function (result) {
                        if (result.Code == 1000) {

                            GrdLayUI.msg(result.Msg, "成功", function () {
                                //获取窗口索引
                                var index = player.getFrameIndex(window.name);
                                //关闭子页面
                                player.close(index);
                                //刷新父页面
                                window.parent.location.reload();
                            });

                        } else {
                            GrdLayUI.msg(result.Msg, "警告");
                        }
                    }
                });
            },

            //取消
            close: function () {
                player.closeAll();
            }
        },
        //工具
        tools: {
        }
    };
    projectInfoModifyLogic.init();
});
